package org.kl.wx.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.BasicFile;
import org.kl.bf.service.basic.BasicFileService;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.wx.entity.MaterialActicle;
import org.kl.wx.repository.MaterialActicleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class MaterialActicleService {
	private MaterialActicleDao materialActicleDao;
	private BasicFileService basicFileService;

	public MaterialActicle getMaterialActicle(String id) {
		return materialActicleDao.find(MaterialActicle.class, id);
	}

	public void saveMaterialActicle(MaterialActicle entity) {
		materialActicleDao.save(entity);
	}

	public void deleteMaterialActicle(String id) {
		materialActicleDao.delete(id);
	}

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				MaterialActicle ma = materialActicleDao.find(MaterialActicle.class, id);
				BasicFile bf = basicFileService.getBasicFile(ma.getThumbMediaId());
				basicFileService.deleteFile(bf.getFilePath());
				basicFileService.deleteBasicFile(bf.getId());
				materialActicleDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	public List<MaterialActicle> getAllMaterialActicle() {
		return materialActicleDao.findAll();
	}

	/**
	 * 按条件查询
	 */
	public List<MaterialActicle> getMaterialActicle(Map<String, Object> searchParams) {
		Specification<MaterialActicle> spec = buildSpecification(searchParams);
		return materialActicleDao.findAll(spec);
	}

	/**
	 * 按条件分页查询
	 */
	public Page<MaterialActicle> getMaterialActicle(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<MaterialActicle> spec = buildSpecification(searchParams);

		return materialActicleDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<MaterialActicle> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<MaterialActicle> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), MaterialActicle.class);
		return spec;
	}

	@Autowired
	public void setMaterialActicleDao(MaterialActicleDao materialActicleDao) {
		this.materialActicleDao = materialActicleDao;
	}

	@Autowired
	public void setBasicFileService(BasicFileService basicFileService) {
		this.basicFileService = basicFileService;
	}

}
